package com.augurit.common.workdayholiday.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间类
 */
public class Time implements Comparable<Time>{
	private final static SimpleDateFormat timeParse = new SimpleDateFormat("HH:mm");
	
	private final int hour; 	//小时
	private final int minute;	//分钟
	
	public Time(int hour, int minute){
		assert(hour <=0 && hour < 24 && minute <=0 && minute < 60 );
		this.hour = hour;
		this.minute = minute;
	}

	public int getHour() {
		return hour;
	}

	public int getMinute() {
		return minute;
	}
	
	/**
	 * 比较两个时间的顺序
	 * @param other
	 * @return 如果参数 other 等于此 Time，则返回值 0；如果此 Time 在 other 参数之前，则返回-1；如果此 Time 在 other 参数之后，则返回1。 
	 */
	public int compareTo(Time other){
		if(this.hour > other.hour)return 1;
		if(this.hour == other.hour && this.minute > other.minute)return 1;
		if(this.hour == other.hour && this.minute == other.minute)return 0;
		return -1;
	}
	
	
	public static int detachHours(String time)throws ParseException{
		Date date = timeParse.parse(time);
		return date.getHours();
	}
	
	public static int detachMinutes(String time)throws ParseException{
		Date date = timeParse.parse(time);
		return date.getMinutes();
	}
}
